import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Home',
        component: Home,
        redirect: '/newart',

        children: [{
                path: '/newart',
                component: () =>
                    import ('../views/home/NewArt.vue')
            },
            {
                path: '/hotart',
                component: () =>
                    import ('../views/home/HotArt.vue')
            },
            {
                path: '/allart',
                component: () =>
                    import ('../views/home/AllArt.vue')
            },
            {
                path: '/artDetail/:id',
                component: () =>
                    import ('../views/home/artDetail.vue')
            }
        ]
    },
    {
        path: '/admin',
        name: 'Admin',
        component: () =>
            import ('../views/Admin.vue'),
        children: [{
                path: '/admin/adminPage',
                component: () =>
                    import ('../views/admin/AdminPage.vue'),
                redirect: '/admin/adminPage/user',
                children: [{
                        path: '/admin/adminPage/user',
                        component: () =>
                            import ('../views/admin/adminpage/User.vue')
                    },

                    {
                        path: '/admin/adminPage/article',
                        component: () =>
                            import ('../views/admin/adminpage/Article.vue')
                    }

                ]
            },
            {
                path: '/admin/userPage',
                component: () =>
                    import ('../views/admin/UserPage.vue'),
                redirect: '/admin/userPage/fond',
                children: [{
                    path: '/admin/userPage/fond',
                    component: () =>
                        import ('../views/admin/userPage/Fond.vue')
                }]
            }
        ]
    }
]

const router = new VueRouter({
        routes
    })
    // 放置前置路由守卫  来做登陆拦截
router.beforeEach((to, from, next) => {
    if (!to.path.includes('admin')) return next()
    if (localStorage.token) return next()
        // 走到这里代表要请求admin  并且还没有登陆
    Vue.prototype.$message.error('请先登陆')
    setTimeout(() => {
        router.go(0)
    }, 500);
})
export default router